<template>
    <section>
      <nav class="nav navbar-fixed-top col-xs-12">
        <span class="navLeft" @click="refresh">ele.me</span>
        <router-link :to="{name: 'login'}" class="pull-right navRight" v-if="isTrue2">登录|注册</router-link>
        <router-link :to="{name: 'mineed'}" class="pull-right navRight" v-if="!isTrue2"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></router-link>
      </nav>
      <div class="cityPosi">
        <span class="cityPosiOne">当前定位城市:</span>
        <span class="cityPosiTwo">定位不准时, 请在城市列表中选择</span>
      </div>
      <!--城市定位-->
      <div class="myCity">
        <router-link :to="{name:'citySearch', query: {Mycity: myCity.name, index: myCity.id}}">{{myCity.name}}</router-link>
        <div><span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span></div>
      </div>
      <div class="hoteCity"><p>热门城市</p></div>
        <div class="row hotCitys">
          <div class="col-xs-3" v-for="(city, i) in hotCity">
            <router-link :to="{path:'citySearch', query: {Mycity: city.name, index:city.id}}">{{city.name}}</router-link>
          </div>
        </div>
      <!--全部城市部分-->
      <div class="body row allCity" v-cloak v-for="city in letter">
        <div class="letter">{{city.letter}}</div>
        <div v-for="(citys, i) in city.city" class="col-xs-3 citys" style="overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
          <router-link :to="{path:'citySearch', query: {Mycity: citys.name, index:citys.id}}">{{citys.name}}</router-link>
        </div>
      </div>
      <XBT v-if="animation"></XBT>
    </section>
</template>

<script>
  import Vue from 'vue'
  import XBT from "../tackOut/XBT";
export default {
  name: "CityList",
  components: {XBT},
  data(){
    return {
      myCity:'',//当前城市名称
      hotCity:[],//存储热门城市
      letter:[],// 存储各个城市
      localMsg:'',//判断是否为登录状态
      isTrue2:true, // 判断登录状态
      animation: true//控制雪碧图
    }
  },
  created(){
    this.animation = true;
    // 发起网络请求获取城市位置
    Vue.axios.get('https://elm.cangdu.org/v1/cities?type=guess', null).then((res) => {
      this.animation = false;
      this.myCity = res.data;
    });
    //发起网络请求获取热门城市
    Vue.axios.get('https://elm.cangdu.org/v1/cities?type=hot', null).then((res) => {
      this.animation = false;
      this.hotCity = res.data
    });
    //发起网络请求获取全部城市
    Vue.axios.get('https://elm.cangdu.org/v1/cities?type=group', null).then((res) => {
      this.animation = false;
      // console.log(res.data)
      let arr = Object.keys(res.data).sort();
      for (let i = 0; i < arr.length; i++) {
        const obj = {letter: arr[i], city: res.data[arr[i]]};
        this.letter.push(obj);
      }
    });
    // console.log(this.letter)
  },
  mounted(){
    // 判断是否登录
      this.localMsg = localStorage.getItem('personObj');
      // console.log(this.localMsg);
     if (this.localMsg == null){
       this.isTrue2 = true
     } else{
       this.isTrue2 = false
     }
  },
  methods:{
    refresh(){
      location.reload();
    }
  }
}
</script>

<style scoped>
  [v-cloak]{
    display: none;
  }
  section{
    width: 100%;
    height: 100%;
    font-size: 0.13rem;
  }
.nav{
  width: 100%;
  height: 0.35rem;
  background-color: #298eeb;
  line-height: 0.35rem;
}
  .navLeft{
    margin-left: 0.1rem;
    color: white;
  }
  .navRight{
    margin-right: 0.1rem;
    color: white;
  }
  .cityPosi{
    width: 100%;
    margin-top: 0.45rem;
    padding-bottom: 0.05rem;
    border-bottom: lightgray 0.01rem solid;
    display: flex;
    justify-content: space-between;
  }
  .cityPosiOne{
    margin-left: 0.1rem;
  }
  section .cityPosiTwo{
    margin-right: 0.1rem;
    font-size: 0.12rem;
  }
  .myCity{
    width: 100%;
    height: 0.35rem;
    line-height: 0.35rem;
    display: flex;
    justify-content: space-between;
    border-bottom: lightgray 0.02rem solid;
  }
  .myCity > a{
    margin-left: 0.1rem;
    font-size: 0.16rem;
  }
  .myCity > div{
    margin-right: 0.1rem;
  }
  .hoteCity{
    width: 100%;
    height: 0.3rem;
    line-height: 0.3rem;
    border-top: lightgray 0.02rem solid;
    margin-top: 0.08rem;
  }
  .hoteCity > p{
    margin-left: 0.1rem;
  }
  .body a{
    color: #000;
  }
  .hotCitys{
    border-top: lightgray 0.01rem solid;
    text-align: center;
    border-bottom: lightgray 0.01rem solid;
  }
  .allCity{
    border-top: lightgray 0.01rem solid;
    border-bottom: lightgray 0.01rem solid;
  }
  .hotCitys>*{
    height: 0.3rem;
    line-height: 0.3rem;
    border-right: lightgray 0.01rem solid;
    border-bottom: lightgray 0.01rem solid;
  }
  .letter{
    height: 0.3rem;
    line-height: 0.3rem;
    padding-left: 0.25rem;
    margin-top: 0.08rem;
    border-top: lightgray 0.02rem solid;
    border-bottom: lightgray 0.01rem solid;
  }
  .citys{
    height: 0.3rem;
    line-height: 0.3rem;
    border-right: lightgray 0.01rem solid;
    border-bottom: lightgray 0.01rem solid;
    text-align: center;
  }
</style>
